{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-07-02T06:24:36.556701800Z",
     "start_time": "2023-07-02T06:24:36.516764Z"
    }
   },
   "outputs": [],
   "source": [
    "import zipfile\n",
    "import shutil\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 1、压缩单个文件\n",
    "with zipfile.ZipFile('List.zip', 'w', compression=zipfile.ZIP_DEFLATED) as zf:\n",
    "    filepath = r'C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\\List.py'\n",
    "    zf.write(filepath, arcname=os.path.basename(filepath))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T06:27:48.198003400Z",
     "start_time": "2023-07-02T06:27:48.158412900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "'C:\\\\Users\\\\17634\\\\PycharmProjects\\\\automate-the-boring-stuff\\\\3、组织文件\\\\temp2.zip'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、压缩整个文件夹\n",
    "# (1) 不把压缩目录作为归档的根目录\n",
    "shutil.make_archive(r'temp2', 'zip', root_dir=r'../Test')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T02:24:54.561281900Z",
     "start_time": "2023-06-23T02:24:54.532286700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "'C:\\\\Users\\\\17634\\\\PycharmProjects\\\\automate-the-boring-stuff\\\\3、组织文件\\\\temp3.zip'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (2) 把压缩目录作为归档的根目录\n",
    "shutil.make_archive(r'temp3', 'zip', root_dir=r'..', base_dir='Test')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T02:24:55.762549200Z",
     "start_time": "2023-06-23T02:24:55.715519700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 3、解压缩文件夹\n",
    "shutil.unpack_archive('List.zip', '.') #或者ZipFile.extractall(path)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 案例一：选择文件夹中部分文件进行压缩"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def zip_dir(src_dir, zipfile_name=None):\n",
    "    \"\"\"\n",
    "    压缩目录为.zip文件\n",
    "    :param src_dir: 目录路径，str\n",
    "    :param zipfile_name: 压缩文件名字，str，不指定则和src_dir相同\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    if zipfile_name is None:\n",
    "        zipfile_name = f\"{os.path.basename(src_dir)}.zip\"\n",
    "    with zipfile.ZipFile(zipfile_name, 'w', zipfile.ZIP_DEFLATED) as zf:\n",
    "        for dirpath, subdirs, filenames in os.walk(src_dir):\n",
    "            for filename in filenames:\n",
    "                filepath = os.path.join(dirpath, filename) #当前文件路径\n",
    "                ############之间写过滤文件代码##############\n",
    "                ############之间写过滤文件代码##############\n",
    "        # (1)、zipfile的write()方法第二个参数arcname解释\n",
    "                # 如此时filepath = r'../Test/List.py',\n",
    "                # 替换后为Test\\List.py\n",
    "                # 再如当filepath = r'C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\\demo\\01.py',\n",
    "                # 替换后为Test\\demo\\01.py\n",
    "                arcname = filepath.replace(os.path.dirname(src_dir) + os.path.sep, '')\n",
    "                zf.write(filepath, arcname=arcname)\n",
    "        # (2)、是否压缩空文件夹\n",
    "            # 以下为压缩空文件夹代码，且把该目录作为压缩文件的根目录\n",
    "            if len(subdirs) == 0 and len(filenames) == 0:\n",
    "                arcname = dirpath.replace(os.path.dirname(src_dir) + os.path.sep, '')\n",
    "                zf.write(dirpath, arcname=arcname)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-27T08:02:01.335616700Z",
     "start_time": "2023-06-27T08:02:01.310625Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "# 注：ZipFile的write()可以写入文件夹路径，但写入后是空文件夹；可直接写入多重空文件夹路径\n",
    "# 直接写入多重空文件夹路径\n",
    "with zipfile.ZipFile('newZip3.zip', 'w', zipfile.ZIP_DEFLATED) as zf:\n",
    "    zf.write(r'C:\\Users\\17634\\PycharmProjects\\automate-the-boring-stuff\\Test\\b\\b', arcname=r'Test\\b\\b')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T02:11:29.772324600Z",
     "start_time": "2023-06-23T02:11:29.748172200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T02:28:35.184467100Z",
     "start_time": "2023-06-23T02:28:35.130469500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 案例二：解压文件中文名称乱码"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from zipfile import ZipFile\n",
    "from pathlib import Path\n",
    "\n",
    "def extract_zip(src, dst=None, include=True):\n",
    "    \"\"\"\n",
    "    src     : zip文件的路径\n",
    "    dst     : 要压缩到哪个文件夹，默认压缩到zip文件所在的文件夹下\n",
    "    include : 是否将zip文件名作为压缩文件的根目录\n",
    "    \"\"\"\n",
    "    with ZipFile(src) as zip_file:\n",
    "        zip_file.setpassword()\n",
    "        name_to_info = zip_file.NameToInfo\n",
    "        # copy map first\n",
    "        for name, info in name_to_info.copy().items():\n",
    "            real_name = name.encode('cp437').decode('gbk')\n",
    "            if real_name != name:\n",
    "                info.filename = real_name\n",
    "                del name_to_info[name]\n",
    "                name_to_info[real_name] = info\n",
    "        if dst is None:\n",
    "            dst = Path(src).parent\n",
    "        else:\n",
    "            dst = Path(dst)\n",
    "        if include:\n",
    "            dst = dst / Path(src).stem\n",
    "            zip_file.extractall(str(dst))\n",
    "        else:\n",
    "            zip_file.extractall(dst)\n",
    "\n",
    "file_path = r\"C:\\Users\\17634\\Desktop\\集成算法.zip\"\n",
    "extract_zip(file_path)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-27T07:41:29.462750800Z",
     "start_time": "2023-06-27T07:41:29.436484Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-23T02:33:08.926559800Z",
     "start_time": "2023-06-23T02:33:08.902563900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
